<a onclick="fnClickAddRow();" href="javascript:void(0);" class="btn btn-primary">添加图文</a>
<div id="item_custom_media_pager" class="jqpager"></div>
<table class="table table-striped table-bordered" id="mainMediaTable" style="padding-top: 5px;" border="0">
	<thead>
		<tr>
			<th><input type="checkbox" id="all" title="全选/反选" style="margin-right: 4px; vertical-align: middle;"/></th>
			<th><span style="vertical-align: middle;">&nbsp;&nbsp;素材标题</span></th>
			<th>摘要</th>
			<th>访问地址</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody class="mediaLotGrid"></tbody>
</table>
<!-- html 模板文件 -->
<script type="text/template" id="custom_media_table_tr_tpl">
{{each list as item i}}
		<tr class="package" id="tr_id_{{item.id}}" data-id="{{item.mediaId}}" data-title="{{item.title}}" data-image="{{item.picUrl}}" data-digest="{{item.desc}}" data-url="{{item.viewUrl}}">
	<td><input type="checkbox" class="commchk" id="checkbox_{{item.id}}" style="margin-right: 4px; vertical-align: middle;"/></td>
			<td>{{item.title}}</td>
            <td>{{item.desc}}</td>
            <td><a href="{{item.viewUrl}}" target="_blank">打开链接</a></td>
<td><a id="edit#{{item.id}}" href="javascript:void(0)">编辑</a></td>
		</tr>
{{/each}}
</script>
<script type="text/template" id="custom_media_table_noresult_tr_tpl">
	<tr class="package"><td class="center" colspan="5">没有数据</td></tr>
</script>
<script type="text/javascript">
var TableOpt = {
	edit : function(param){
		var id = param[1];
		fnClickAddRow(id);
		return false;
	},	
	init : function(){
		var optArray = new Array();
		optArray.push(this.edit);
		return optArray;
	}
}

var params = {};
var mediaTable = new obz.TableView("item_custom_media_pager", "#(webctx)/media/list", params, function(resp){
	$(".mediaLotGrid").empty();
	if(resp && resp.list && resp.list.length>0){
		$('.mediaLotGrid').html(template('custom_media_table_tr_tpl', resp));
		TBatch.tableId = "mainMediaTable";
		TBatch.checkCheckbox();
		TBatch.initCheckboxClick();
		TBatch.initOptEvent(TableOpt.init());
	}else{
		$(".mediaLotGrid").append($("#custom_media_table_noresult_tr_tpl").html());
	}
});
mediaTable.init();

function fnClickAddRow(id){
	var url = "#(webctx)/media/add";	
	if(id){
		url = "#(webctx)/media/add?id="+id;
	}
	log("===============url:" + url);
	BootstrapDialog.show({
		size: BootstrapDialog.SIZE_WIDE,
		title: "新增图文素材",
        message: $('<div></div>').load(url),
        buttons: [ {
            label: '关闭',
            action: function(dialogItself){
                dialogItself.close();
            }}, {
            	label: '确定',
            	cssClass : "btn-primary",
            	action: function(self){
            		var title = $("#m_title").val();
            		if(title == null || title == ""){
            			obz.warn("请输入素材标题");
            			return;
            		}
            		var desc = $("#m_desc").val();
            		var url = $("#m_url").val();
            		if(url == null || url ==""){
            			obz.warn("请输入素材链接地址");
            			return;
            		}
            		
            		var mediaImgId = $("div.imagemsg_item").attr("data-media-id");
            		var mediaImgUrl = $("div.imagemsg_item").attr("data-image");
            		log("=====mediaImgId:" + mediaImgId + ",mediaImgUrl:" + mediaImgUrl);
            		if(mediaImgUrl==null || mediaImgUrl == ""){
            			obz.warn("请选择封面图片");
            			return;
            		}
            		
            		var params = {};
            		params.id = $("#media_id").val();
            		log("=========id:" + params.id)
            		params.title = title;
            		params.desc = desc;
            		params.picUrl = mediaImgUrl;
            		params.viewUrl = url;
            		obz.ajaxJson("#(webctx)/media/save", params, function(resp){
            			if(resp.state=="ok"){
            				self.close();
            				mediaTable.init();
            			}
            		});
            	}
            }]
    });
}
</script>